const { commonProps } = require("../../miniprogram_npm/@vant/weapp/field/props");

const app = getApp()
var uploadImage = require('../../utils/uploadFile.js');//地址换成你自己存放文件的位置
var util = require('../../utils/util.js');
var uploadVideo = require('../../utils/upFiles.js');



// pages/evaluation/evaluation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'',
    value: '',    //产品满意度的数据
    description: '',   //使用案例描述
    name:'',
    phone:'',
    orderid:'',
    imgsrc:'',      //产品图片
    sceneimgsrc:'',   //案例场景图片
    show: false,   //产品上传图片/视频弹出层显示
    show1: false,   //场景使用上传图片/视频弹出层显示
    imageshow:false,
    videoshow:false,
    imageshow1:false,
    videoshow1:false,

    upImgArr:[],
    upImgArr1:[],

     //上传视频模块
     upVideoArr: [], //存视频
     upVideoArr1:[],
     upFilesBtn: true,
     maxUploadLen:1,  //限制上传数量

    //  积分规则
    integralRule:[]
  },

  // 产品
  // 上传图片/视频弹出层显示
  showPopup() {
    
    console.log('imgsrc',this.data.imgsrc)
    if(this.data.imgsrc != ''){
      wx.showToast({
        title: '请删除原有图片！',
        icon:'none'
      })
      return 
    }else{
      this.setData({ show: true });
    }
  },
  // 上传图片/视频弹出层隐藏
  onClose() {
    this.setData({ show: false });
  },
  // 使用场景
  // 上传图片/视频弹出层显示
  showPopup1() {
    if(this.data.sceneimgsrc != ''){
      wx.showToast({
        title: '请删除原有图片！',
        icon:'none'
      })
      return 
    }else{
      this.setData({ show1: true });
    }
  },
  // 上传图片/视频弹出层隐藏
  onClose1() {
    this.setData({ show1: false });
  },
  // 姓名
  inputName(event){
    console.log('name',event.detail.value)
    this.setData({
      name:event.detail.value
    })
  },
  // 电话
  inputPhone(event){
    console.log('phone',event.detail.value)
    this.setData({
      phone:event.detail.value
    })
  },
  // 案例描述
  inputDescription(event){
    console.log('description',event.detail.value)
    this.setData({
      description:event.detail.value
    })
  },
  // 产品满意度的修改
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },
  
  // 提交评价的数据
  submit(){
    
    var that = this
    if(that.data.name && that.data.phone && that.data.description && that.data.sceneimgsrc ){
          wx.request({
          url: getApp().globalData.baseUrl + '/wago/saoma/add',
          data: {
            userid:getApp().globalData.openid,
            name:that.data.name,
            phone:that.data.phone,
            id:that.data.orderid,  //orderid  订单id
            miaoshu:that.data.description,
            satisfaction:that.data.value,   //满意度
            scene:that.data.sceneimgsrc      //案例使用场景图片
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
            // success
            console.log('res',res)
            
            if(res.data.flag){
              wx.showToast({
                title: res.data.message,
                icon:'none'
              })
              that.searchJifen()   //请求接口查询积分规则，然后增加积分
              wx.navigateTo({
                url: '/pages/whetherEvaluate/whetherEvaluate'
              })
            
            }else{
              wx.showToast({
                title: res.data.message,
                icon:'none'
              })
            }
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
    }else{
      wx.showToast({
        title: '请把信息填写完毕再提交',
        icon:'none'
      })
    }

  },
  // 请求接口查询积分规则
  searchJifen(){
    console.log('查询积分规则接口')
    var that = this
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/jifen/findJifenControl',
      data: {
        id:'6'
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        console.log('查询积分规则',res)
        if(res.data.flag){
          that.setData({
            integralRule:res.data.data
          })
          that.changeJifen()   //修改积分
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  // 修改积分
  changeJifen(){
    var that = this
    var time = new Date().getTime()
    console.log('time',time)
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/jifen/changeJifen',
      data: {
        userid:getApp().globalData.openid,
        changejifen:that.data.integralRule.changejifen,
        changename:that.data.integralRule.name,
        changetime:time
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        console.log('修改积分返回的数据',res)
        if(res.data.flag){
          wx.showToast({
            title: '已为您添加积分',
            icon:'none'
          })
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  // 产品视频上传
 // 选择图片或者视频
 uploadFiles(e) {
   
  var that = this,
    type = e.currentTarget.dataset.type,
    maxUploadLen = that.data.maxUploadLen;
  that.onClose()
  if (type == 'image') {
    // upFiles.chooseImage(that, maxUploadLen);
    wx.chooseImage({
      count: maxUploadLen,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        var imgArr = that.data.upImgArr || [];
        let arr = res.tempFiles;
        arr.map(function (v, i) {
          v['progress'] = 0;
          imgArr.push(v)
        })
        that.setData({
          upImgArr: imgArr
        })
        
        


        
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var nowTime = util.formatTime(new Date());
        //支持多图上传
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          //显示消息提示框
          wx.showLoading({
             title: '上传中' + (i + 1) + '/' + res.tempFilePaths.length,
             mask: true
          })

          //上传图片
          //你的域名下的/cbb文件下的/当前年月日文件下的/图片.png
          //图片路径可自行修改
         uploadImage(res.tempFilePaths[i], 'images/' + nowTime + '/',
             function (result) {
              //  console.log("======上传成功图片地址为：", result);
               //做你具体的业务逻辑操作
               that.setData({
                 imgsrc:result,
                 imageshow:true,
                 videoshow:false
               })
               
                wx.hideLoading();
             }, function (result) {
               console.log("======上传失败======", result);
               //做你具体的业务逻辑操作
               wx.showToast({
                 title: result,
                 icon:'error'
               })
               wx.hideLoading()
             }
          )
        }
      },
    });

  } else if (type == 'video') {
    // upFiles.chooseVideo(that, maxUploadLen);
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      compressed: true,
      camera: 'back',
      success: function (res) {
        let videoArr = that.data.upVideoArr || [];
        let videoInfo = {};
        videoInfo['tempFilePath'] = res.tempFilePath;
        videoInfo['size'] = res.size;
        videoInfo['height'] = res.height;
        videoInfo['width'] = res.width;
        videoInfo['thumbTempFilePath'] = res.thumbTempFilePath;
        videoInfo['progress'] = 0;
        videoArr.push(videoInfo)
        that.setData({
          upVideoArr: videoArr
        })
       




        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var nowTime = util.formatTime(new Date());
        //支持多图上传
        // for (var i = 0; i < res.tempFilePaths.length; i++) {
          //显示消息提示框
          // wx.showLoading({
          //    title: '上传中' + (i + 1) + '/' + res.tempFilePaths.length,
          //    mask: true
          // })



          //上传图片
          //你的域名下的/cbb文件下的/当前年月日文件下的/图片.png
          //图片路径可自行修改
          uploadVideo(res.tempFilePath, 'videos/' + nowTime + '/',
             function (result) {
              //  console.log("======上传成功视频地址为：", result);
               //做你具体的业务逻辑操作
               that.setData({
                 imgsrc:result,
                 videoshow:true,
                 imageshow:false
               })
               
                wx.hideLoading();
             }, function (result) {
               console.log("======上传失败======", result);
               //做你具体的业务逻辑操作
               wx.showToast({
                 title: result,
                 icon:'error'
               })
               wx.hideLoading()
             }
          )
        // }





      }
    })
  }
},
// 删除产品图片
delImgFile(){
  let that = this;
  wx.showModal({
    title: '温馨提示',
    content: '您确认要删除吗？',
    success(res) {
      if (res.confirm) {
        that.setData({
          imgsrc:'',
          upImgArr:[]
        })

        
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
},
// 删除上传视频
delproImageFile(e) {
  let that = this;
  wx.showModal({
    title: '温馨提示',
    content: '您确认要删除吗？',
    success(res) {
      if (res.confirm) {
        that.setData({
          imgsrc:'',
          upVideoArr:[]
        })

        
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
},

  // 预览图片
  previewImg(e) {
    let that = this,
      imgsrc = e.currentTarget.dataset.presrc,
      arr = that.data.upImgArr,
      preArr = [];
    arr.map(function (v, i) {
      preArr.push(v.path)
    })
    //console.log(preArr)
    wx.previewImage({
      current: imgsrc,
      urls: preArr
    })
  },

  
  // 产品视频上传
 // 选择图片或者视频
 uploadVideoFiles(e) {
   
  var that = this,
    type = e.currentTarget.dataset.type,
    maxUploadLen = that.data.maxUploadLen;
  that.onClose1()
  if (type == 'image') {
    // upFiles.chooseImage(that, maxUploadLen);
    wx.chooseImage({
      count: maxUploadLen,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        var imgArr = that.data.upImgArr1 || [];
        let arr = res.tempFiles;
        arr.map(function (v, i) {
          v['progress'] = 0;
          imgArr.push(v)
        })
        that.setData({
          upImgArr1: imgArr
        })
        
       

        
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var nowTime = util.formatTime(new Date());
        //支持多图上传
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          //显示消息提示框
          wx.showLoading({
             title: '上传中' + (i + 1) + '/' + res.tempFilePaths.length,
             mask: true
          })

          //上传图片
          //你的域名下的/cbb文件下的/当前年月日文件下的/图片.png
          //图片路径可自行修改
         uploadImage(res.tempFilePaths[i], 'images/' + nowTime + '/',
             function (result) {
              //  console.log("======上传成功图片地址为：", result);
               //做你具体的业务逻辑操作
               that.setData({
                 sceneimgsrc:result,
                 imageshow1:true,
                 videoshow1:false
               })
               
                wx.hideLoading();
             }, function (result) {
               console.log("======上传失败======", result);
               //做你具体的业务逻辑操作
               wx.showToast({
                 title: result,
                 icon:'error'
               })
               wx.hideLoading()
             }
          )
        }
      },
    });

  } else if (type == 'video') {
    // upFiles.chooseVideo(that, maxUploadLen);
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      compressed: true,
      camera: 'back',
      success: function (res) {
        let videoArr = that.data.upVideoArr1 || [];
        let videoInfo = {};
        videoInfo['tempFilePath'] = res.tempFilePath;
        videoInfo['size'] = res.size;
        videoInfo['height'] = res.height;
        videoInfo['width'] = res.width;
        videoInfo['thumbTempFilePath'] = res.thumbTempFilePath;
        videoInfo['progress'] = 0;
        videoArr.push(videoInfo)
        that.setData({
          upVideoArr1: videoArr
        })
        

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var nowTime = util.formatTime(new Date());
          //上传视频
          //你的域名下的/cbb文件下的/当前年月日文件下的/图片.png
          //图片路径可自行修改
          uploadVideo(res.tempFilePath, 'videos/' + nowTime + '/',
             function (result) {
               console.log("======上传成功视频地址为：", result);
               //做你具体的业务逻辑操作
               that.setData({
                 sceneimgsrc:result,
                 videoshow1:true,
                 imageshow1:false
               })
               
                wx.hideLoading();
             }, function (result) {
               console.log("======上传失败======", result);
               //做你具体的业务逻辑操作
               wx.showToast({
                 title: result,
                 icon:'error'
               })
               wx.hideLoading()
             }
          )

      }
    })
  }
},
// 场景图片删除
delVideoFile(){
  let that = this;
  wx.showModal({
    title: '温馨提示',
    content: '您确认要删除吗？',
    success(res) {
      if (res.confirm) {
        that.setData({
          sceneimgsrc:'',
          upImgArr1:[]
        })

        
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
},
// 场景删除
delsceneVideoFile(e) {
  let that = this;
  wx.showModal({
    title: '温馨提示',
    content: '您确认要删除吗？',
    success(res) {
      if (res.confirm) {
        that.setData({
          sceneimgsrc:'',
          upVideoArr1:[]
        })

      
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
},
// 预览图片
previewImg1(e) {
  let that = this,
    imgsrc = e.currentTarget.dataset.presrc,
    arr = that.data.upImgArr,
    preArr = [];
  arr.map(function (v, i) {
    preArr.push(v.path)
  })
  //console.log(preArr)
  wx.previewImage({
    current: imgsrc,
    urls: preArr
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('评价',options)
    this.setData({
      title:options.title,
      orderid:options.orderid
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
})